/*
 * @FileName: [DydlAppController.java]
 * 
 * @Package com.bci.pwtz.controller.app
 * 
 * 
 * Copyright (c) 2011-2015 BCINFO Technology Limited Com.
 * All rights reserved.
 * 
 * This software is the confidential and proprietary
 * information of BCINFO Technology Limited Company
 * ("Confidential Information"). You shall not disclose
 * such Confidential Information and shall use it only
 * in accordance with the terms of the contract agreement
 * you entered into with RKY.
 * 
 * $Rev$
 * $LastChangedDate$
 * $LastChangedBy$
 * 
 * @category ALUMNI
 * 
 * @version 1.1
 * 
 * @author $Author$
 * 
 * Change History:[Formatter: author date description] <br/>
 * 1
 * 2
 * 3
 */

package com.bci.pwtz.controller.app;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bci.pwtz.common.DeviceDictConstants;
import com.bci.pwtz.common.PwtzConstants;
import com.bci.pwtz.common.remote.ListResponse;
import com.bci.pwtz.common.remote.ObjResponse;
import com.bci.pwtz.common.remote.ReturnCode;
import com.bci.pwtz.common.remote.req.DydlIdReq;
import com.bci.pwtz.common.remote.req.DydlListReq;
import com.bci.pwtz.common.remote.req.DydlReq;
import com.bci.pwtz.common.util.PaginationList;
import com.bci.pwtz.controller.AbstractController;
import com.bci.pwtz.exception.CanNotDeleteException;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.model.Dydl;
import com.bci.pwtz.service.DictionaryAuditService;
import com.bci.pwtz.service.DydlService;

/**
 * 一句话描述 <br/>
 * <p>
 * Description:<br/>
 * <p>
 * TODO 详细描述
 * 
 * <p>
 * For Examples <br/>
 * 
 * <PRE>
 *      TODO 代码使用示例
 * </PRE>
 * <p>
 */
@Controller
@RequestMapping("/api/dydl")
public class DydlAppController extends AbstractController
{
    // 自动扫面
    @Autowired
    private DydlService service;
    @Autowired
    private DictionaryAuditService dictionaryAuditService;
    /**
     * 低压电缆信息的list查询
     * 一句话描述 <br/>
     * <p>Description: TODO(详细描述) 
     * <br/>
     * <p>Author: Administrator<br/>
     * <p>Date: 2014年9月25日-上午10:21:50<br/>
     * <p>
     * @param request
     * @return   
     *
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public ListResponse<Dydl> list(HttpServletRequest request)
    {
        DydlListReq req = null;
        ListResponse<Dydl> resp = new ListResponse<Dydl>();
        PaginationList<Dydl> list = new PaginationList<Dydl>();
        try
        {
            req = (DydlListReq) getJsonToObj(request, DydlListReq.class);
            if (req == null)
            {
                resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
                resp.setReturnMsg("请求为空错误");
                return resp;
            }
            if(req.getTimestamp() == null||req.getCompanyId()==null){
                resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
                resp.setReturnMsg("参数错误");
                resp.setTimestamp(req.getTimestamp());
                return resp;
            }
            if (req.getSecretKey() == null)
            {
                resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
                resp.setReturnMsg("密钥为空");
                resp.setTimestamp(req.getTimestamp());
                return resp;
            }
            if (!validateSecretKey(req.getSecretKey(), req.getTimestamp()))
            {
                resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
                resp.setReturnMsg("密钥错误");
                resp.setTimestamp(req.getTimestamp());
                return resp;
            }
            if (req.getUserId() == null)
            {
                resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
                resp.setReturnMsg("用户ID为空");
                resp.setTimestamp(req.getTimestamp());
                return resp;
            }
            if (StringUtils.isBlank(req.getToken()) || !validateToken(req.getToken(), req.getUserId()))
            {
                resp.setReturnCode(ReturnCode.TOKEN_INVALID);
                resp.setReturnMsg("票据无效");
                resp.setTimestamp(req.getTimestamp());
                return resp;
            }
            list = (PaginationList<Dydl>) service.findDydl(req.getDeviceName(), req.getCompanyId(), PwtzConstants.PORTAL_TYPE_APP,req.getMedLineCodes(),null, req.getBeginTime(), null, req.getPageSize(),req.getPage());
            if (list != null)
            {
                resp.setReturnCode(ReturnCode.SUCCESS);
                resp.setReturnMsg("查询低压电缆列表信息成功");
                resp.setTimestamp(req.getTimestamp());
                resp.setObjs(list);
            }else{
                resp.setReturnCode(ReturnCode.INTERFACE_INVOKE_ERROR);
                resp.setReturnMsg("查询低压电缆列表信息调用接口返回失败");
                resp.setTimestamp(req.getTimestamp());
            }
        }
        catch (PwtzDatabaseException e)
        {
            resp.setReturnCode(ReturnCode.DB_EXCEPTION);
            resp.setReturnMsg("数据库操作异常");
            resp.setTimestamp(req.getTimestamp());
        }
        catch (Exception e)
        {
            resp.setReturnCode(ReturnCode.UNKOWN_ERROR);
            resp.setReturnMsg("系统错误");
            resp.setTimestamp(req.getTimestamp());
        }
        return resp;
    }
 
    /**
     * 新增低压电缆信息
     * 一句话描述 <br/>
     * <p>Description: TODO(详细描述) 
     * <br/>
     * <p>Author: Administrator<br/>
     * <p>Date: 2014年9月25日-上午10:22:27<br/>
     * <p>
     * @param request
     * @return   
     *
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public ObjResponse<Dydl> add(HttpServletRequest request)
    {
        ObjResponse<Dydl> resp = new ObjResponse<Dydl>();
        DydlReq req = null;
        try
        {
            req = (DydlReq) getJsonToObj(request, DydlReq.class);
            if (req == null)
            {
                resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
                resp.setReturnMsg("请求为空错误");
                return resp;
            }
            if(req.getTimestamp() == null||req.getObj()==null){
                resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
                resp.setReturnMsg("参数错误");
                resp.setTimestamp(req.getTimestamp());
                return resp;
            }
            if (req.getSecretKey() == null)
            {
                resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
                resp.setReturnMsg("密钥为空");
                resp.setTimestamp(req.getTimestamp());
                return resp;
            }
            if (!validateSecretKey(req.getSecretKey(), req.getTimestamp()))
            {
                resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
                resp.setReturnMsg("密钥错误");
                resp.setTimestamp(req.getTimestamp());
                return resp;
            }
            if (req.getUserId() == null)
            {
                resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
                resp.setReturnMsg("用户ID为空");
                resp.setTimestamp(req.getTimestamp());
                return resp;
            }
            if (StringUtils.isBlank(req.getToken()) || !validateToken(req.getToken(), req.getUserId()))
            {
                resp.setReturnCode(ReturnCode.TOKEN_INVALID);
                resp.setReturnMsg("票据无效");
                resp.setTimestamp(req.getTimestamp());
                return resp;
            }
            int result=0;
            Dydl dydl = null;
            if(service.load(req.getObj().getDeviceid())!=null){
                result=service.update(req.getObj());
            }else{
                dydl=service.add(req.getObj());
            }
            if (dydl != null||result>0)
            {
                Dydl rtObj=service.load(req.getObj().getDeviceid());
                dictionaryAuditService.add(DeviceDictConstants.DYDL_MANUFACTURE_FACTORY_DICT_ID, req.getObj().getProductionfactory());
                dictionaryAuditService.add(DeviceDictConstants.DYDL_MODEL_DICT_ID, req.getObj().getModel());
                resp.setReturnCode(ReturnCode.SUCCESS);
                resp.setReturnMsg("新增低压电缆成功");
                rtObj.setLastoperatorid(req.getUserId());
                service.update(rtObj);
                resp.setTimestamp(req.getTimestamp());
                setLog(rtObj.getDeviceid(), rtObj.getCompanyid(), AbstractController.低压电缆, AbstractController.add, "新增低压电缆["+rtObj.getDevicename()+"]成功", req.getUserId());
                resp.setObj(rtObj);
            }else{
                resp.setReturnCode(ReturnCode.INTERFACE_INVOKE_ERROR);
                resp.setReturnMsg("新增低压电缆调用接口返回失败");
                resp.setTimestamp(req.getTimestamp());
            }
        }
        catch (PwtzDatabaseException e)
        {
            resp.setReturnCode(ReturnCode.DB_EXCEPTION);
            resp.setReturnMsg("数据库操作异常");
            resp.setTimestamp(req.getTimestamp());
        }
        catch (Exception e)
        {
            resp.setReturnCode(ReturnCode.UNKOWN_ERROR);
            resp.setReturnMsg("系统错误");
            resp.setTimestamp(req.getTimestamp());
        }
        return resp;
    }

   /**
    * 修改低压电缆信息
    * 一句话描述 <br/>
    * <p>Description: TODO(详细描述) 
    * <br/>
    * <p>Author: Administrator<br/>
    * <p>Date: 2014年9月25日-上午10:22:47<br/>
    * <p>
    * @param request
    * @return   
    *
    */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public ObjResponse<Dydl> update(HttpServletRequest request)
    {
        ObjResponse<Dydl> resp = new ObjResponse<Dydl>();
        DydlReq req = null;
        int result = 0;
        try
        {
            req = (DydlReq) getJsonToObj(request, DydlReq.class);
            if (req == null)
            {
                resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
                resp.setReturnMsg("请求为空错误");
                return resp;
            }
            if(req.getTimestamp() == null||req.getObj()==null){
                resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
                resp.setReturnMsg("参数错误");
                resp.setTimestamp(req.getTimestamp());
                return resp;
            }
            if (req.getSecretKey() == null)
            {
                resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
                resp.setReturnMsg("密钥为空");
                resp.setTimestamp(req.getTimestamp());
                return resp;
            }
            if (!validateSecretKey(req.getSecretKey(), req.getTimestamp()))
            {
                resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
                resp.setReturnMsg("密钥错误");
                resp.setTimestamp(req.getTimestamp());
                return resp;
            }
            if (req.getUserId() == null)
            {
                resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
                resp.setReturnMsg("用户ID为空");
                resp.setTimestamp(req.getTimestamp());
                return resp;
            }
            if (StringUtils.isBlank(req.getToken()) || !validateToken(req.getToken(), req.getUserId()))
            {
                resp.setReturnCode(ReturnCode.TOKEN_INVALID);
                resp.setReturnMsg("票据无效");
                resp.setTimestamp(req.getTimestamp());
                return resp;
            }
            result = service.update(req.getObj());
            if (result > 0)
            {
                Dydl rtObj=service.load(req.getObj().getDeviceid());
                dictionaryAuditService.add(DeviceDictConstants.DYDL_MANUFACTURE_FACTORY_DICT_ID, req.getObj().getProductionfactory());
                dictionaryAuditService.add(DeviceDictConstants.DYDL_MODEL_DICT_ID, req.getObj().getModel());
                resp.setReturnCode(ReturnCode.SUCCESS);
                resp.setReturnMsg("修改低压电缆成功");
                rtObj.setLastoperatorid(req.getUserId());
                service.update(rtObj);
                resp.setTimestamp(req.getTimestamp());
                resp.setObj(rtObj);
                setLog(req.getObj().getDeviceid(), req.getObj().getCompanyid(), AbstractController.低压电缆, AbstractController.update, "修改低压电缆["+req.getObj().getDevicename()+"]成功", req.getUserId());
            }else{
                resp.setReturnCode(ReturnCode.INTERFACE_INVOKE_ERROR);
                resp.setReturnMsg("修改低压电缆调用接口返回失败");
                resp.setTimestamp(req.getTimestamp());
            }
        }
        catch (PwtzDatabaseException e)
        {
            resp.setReturnCode(ReturnCode.DB_EXCEPTION);
            resp.setReturnMsg("数据库操作异常");
            resp.setTimestamp(req.getTimestamp());
        }
        catch (Exception e)
        {
            resp.setReturnCode(ReturnCode.UNKOWN_ERROR);
            resp.setReturnMsg("系统错误");
            resp.setTimestamp(req.getTimestamp());
        }
        return resp;
    }

    /**
     * 删除低压电缆信息
     * 一句话描述 <br/>
     * <p>Description: TODO(详细描述) 
     * <br/>
     * <p>Author: Administrator<br/>
     * <p>Date: 2014年9月25日-上午10:23:18<br/>
     * <p>
     * @param request
     * @return   
     *
     */
     
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public ObjResponse<Dydl> delete(HttpServletRequest request)
    {
        ObjResponse<Dydl> resp = new ObjResponse<Dydl>();
        DydlIdReq req = null;
        int result = 0;
        try
        {
            req = (DydlIdReq) getJsonToObj(request, DydlIdReq.class);
            if (req == null)
            {
                resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
                resp.setReturnMsg("请求为空错误");
                return resp;
            }
            if(req.getTimestamp() == null||req.getDeviceId()==null){
                resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
                resp.setReturnMsg("参数错误");
                resp.setTimestamp(req.getTimestamp());
                return resp;
            }
            if (req.getSecretKey() == null)
            {
                resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
                resp.setReturnMsg("密钥为空");
                resp.setTimestamp(req.getTimestamp());
                return resp;
            }
            if (!validateSecretKey(req.getSecretKey(), req.getTimestamp()))
            {
                resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
                resp.setReturnMsg("密钥错误");
                resp.setTimestamp(req.getTimestamp());
                return resp;
            }
            if (req.getUserId() == null)
            {
                resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
                resp.setReturnMsg("用户ID为空");
                resp.setTimestamp(req.getTimestamp());
                return resp;
            }
            if (StringUtils.isBlank(req.getToken()) || !validateToken(req.getToken(), req.getUserId()))
            {
                resp.setReturnCode(ReturnCode.TOKEN_INVALID);
                resp.setReturnMsg("票据无效");
                resp.setTimestamp(req.getTimestamp());
                return resp;
            }
            result = service.delete(req.getDeviceId());
            if (result > 0)
            {
                Dydl rtObj=service.load(req.getDeviceId());
                resp.setReturnCode(ReturnCode.SUCCESS);
                resp.setReturnMsg("删除低压电缆成功");
                rtObj.setLastoperatorid(req.getUserId());
                service.update(rtObj);
                resp.setObj(rtObj);
                resp.setTimestamp(req.getTimestamp());
                setLog(req.getDeviceId(), rtObj.getCompanyid(), AbstractController.低压电缆, AbstractController.delete, "删除低压电缆["+rtObj.getDevicename()+"]成功", req.getUserId());
            }else{
                resp.setReturnCode(ReturnCode.INTERFACE_INVOKE_ERROR);
                resp.setReturnMsg("删除低压电缆调用接口返回失败");
                resp.setTimestamp(req.getTimestamp()); 
            }
        }
        catch (CanNotDeleteException e)
        {
            resp.setReturnCode(ReturnCode.DB_EXCEPTION);
            resp.setReturnMsg("存在依耐关系，不能删除");
            resp.setTimestamp(req.getTimestamp());
        }
        catch (PwtzDatabaseException e)
        {
            resp.setReturnCode(ReturnCode.DB_EXCEPTION);
            resp.setReturnMsg("数据库操作异常");
            resp.setTimestamp(req.getTimestamp());
        }
        catch (Exception e)
        {
            resp.setReturnCode(ReturnCode.UNKOWN_ERROR);
            resp.setReturnMsg("系统错误");
            resp.setTimestamp(req.getTimestamp());
        }
        return resp;
    }

}
